//
//  LoginViewController.m
//  Recipe
//
//  Created by YaSha_Tom on 2018/12/17.
//  Copyright © 2018年 YaSha-Tom. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisteredViewController.h"
#import "AppDelegate.h"
#import <BmobSDK/Bmob.h>
#import "LoginView.h"

@interface LoginViewController ()<UITextFieldDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) LoginView *loginView;
@property (nonatomic,strong) NSMutableDictionary *parameter;

@end

@implementation LoginViewController
- (LoginView *)loginView {
    if(!_loginView){
        _loginView = [[LoginView alloc]init];
        _loginView.passworkFiled.delegate = self;
        _loginView.nameFiled.delegate = self;
    }
    return _loginView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.parameter = [NSMutableDictionary dictionary];
    self.navigationController.delegate = self;
    [self.view addSubview:self.loginView];
    @weakify(self);   //解决循环引用问题
    [[_loginView.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        [self doNetworking];
    }];
    [[_loginView.registeredButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
        @strongify(self);
        RegisteredViewController *registeredViewController = [[RegisteredViewController alloc]init];
        [self.navigationController pushViewController:registeredViewController animated:YES];
        
    }];
    [self monitorLogin];
}
#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
# pragma -- 检测密码和用户名是否输入
- (void)monitorLogin {
    @weakify(self)
    //用户名输入监听信号
    RACSignal *validAccountSignal = [_loginView.nameFiled.rac_textSignal map:^id(NSString  *value) {
        @strongify(self);
        [self.parameter setValue:value forKey:@"username"];
        return @(value.length > 0);
    }];
    
    //密码输入监听信号
    RACSignal *validPasswordSignal = [_loginView.passworkFiled.rac_textSignal map:^id(NSString *value) {
        @strongify(self);
        [self.parameter setValue:value forKey:@"password"];
        return @(value.length > 0);
    }];
    RACSignal *loginButtonSignal = [RACSignal combineLatest:@[validAccountSignal, validPasswordSignal] reduce:^id(NSNumber *accountValid, NSNumber *passwordValid) {
        return @([accountValid boolValue] && [passwordValid boolValue]);
    }];
    
    RAC(_loginView.loginButton, backgroundColor) = [loginButtonSignal map:^id(id value) {
        @strongify(self);
        if ([value boolValue]) {
            self.loginView.loginButton.enabled = YES;
            return [UIColor qmui_colorWithHexString:@"F55931"];
        } else {
            self.loginView.loginButton.enabled = NO;
            return  [UIColor lightGrayColor];
        }
    }];
}

- (void)doNetworking {
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"userInfo"];
    //查找GameScore表的数据
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (array.count > 0) {
            for (BmobObject *obj in array) {
                if ([[obj objectForKey:@"userName"] isEqualToString:self.parameter[@"username"]]&&[[obj objectForKey:@"passwork"] isEqualToString:self.parameter[@"password"]]) {
                    NSUserDefaults *userdefaults =[NSUserDefaults standardUserDefaults];
                    [userdefaults setBool:YES forKey:@"login"];
                    [userdefaults synchronize];
                    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                    [delegate enterTabBarController];
                }
            }
            [QMUITips showSucceed:@"用户或密码出错" inView:self.view hideAfterDelay:1];
        }else{
            [QMUITips showSucceed:@"没有该用户" inView:self.view hideAfterDelay:1];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
